package com.syezon.wifikey.enhance.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.syezon.wifikey.R;
import com.syezon.wifikey.enhance.EnhanceItemInfo;
import com.syezon.wifikey.view.expandablelayout.ExpandableLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 齐超波 on 2018/9/16.
 */

public class EnhanceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<EnhanceItemInfo> mData = new ArrayList<>();

    public EnhanceAdapter(Context context) {
        this.mContext = context;
    }

    public void setData(List<EnhanceItemInfo> data) {
        if (data != null) {
            mData.clear();
            mData.addAll(data);
            notifyDataSetChanged();
        }
    }

    public void addData(EnhanceItemInfo enhanceItemInfo) {
        if (enhanceItemInfo != null) {
            mData.add(enhanceItemInfo);
            notifyItemInserted(mData.size() - 1);
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == EnhanceItemInfo.TYPE_HARD || viewType == EnhanceItemInfo.TYPE_WIFI) {
            return new EnhanceViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_enhance, parent, false));
        }
        if (viewType == EnhanceItemInfo.TYPE_DIVIDER) {
            return new EnhanceDividerViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_enhance_divider, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder != null) {
            EnhanceItemInfo enhanceItemInfo = mData.get(position);
            int type = enhanceItemInfo.getType();
            if (type == EnhanceItemInfo.TYPE_HARD) {
                EnhanceViewHolder viewHolder = (EnhanceViewHolder) holder;
                boolean opened = viewHolder.expandableLayout.isOpened();
                viewHolder.tvTitle.setText(enhanceItemInfo.getTitle());
                if (enhanceItemInfo.isFinish()) {
                    viewHolder.ivProgress.setVisibility(View.GONE);
                    EnhanceItemAdapter itemAdapter = new EnhanceItemAdapter(mContext);
                    itemAdapter.setData(enhanceItemInfo.getEnhanceList());
                    viewHolder.rvEnhanceList.setAdapter(itemAdapter);
                    if (!opened) {
                       viewHolder.expandableLayout.show();
                    }
                } else {
                    if (opened) {
                        viewHolder.expandableLayout.hideContent();
                    }
                    viewHolder.ivProgress.setVisibility(View.VISIBLE);
                    Animation animation = viewHolder.ivProgress.getAnimation();
                    if (animation == null) {
                        viewHolder.ivProgress.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.rotate));
                    }
                    if (animation != null && !animation.hasStarted()) {
                        viewHolder.ivProgress.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.rotate));
                    }
                }
            }
            if (type == EnhanceItemInfo.TYPE_DIVIDER) {
            }
        }
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        EnhanceItemInfo itemInfo = mData.get(position);
        return itemInfo.getType();
    }

    public  class EnhanceDividerViewHolder extends RecyclerView.ViewHolder {

        public EnhanceDividerViewHolder(View itemView) {
            super(itemView);
        }
    }

    public  class EnhanceViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.expand_enhance)
        ExpandableLayout expandableLayout;
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.iv_progress)
        ImageView ivProgress;
        @BindView(R.id.rv_list)
        RecyclerView rvEnhanceList;

        public EnhanceViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            rvEnhanceList.setLayoutManager(new LinearLayoutManager(mContext));
        }
    }
}
